import {singleClone, Type} from "@utils/Global";

const PREFIX = 'dm_'
const DmActionType = {
  init_dm: PREFIX + 'init_dm',
  insert_dm: PREFIX + 'insert_dm',
  toggle_dm: PREFIX + 'toggle_dm',
  set_value: PREFIX + 'set_value',
  toggle_dm_type: PREFIX + 'toggle_dm_type',
  get_dm_answer_msg: `${PREFIX}get_dm_answer_msg`,
  get_dm_question_msg: `${PREFIX}get_dm_question_msg`,
  get_dm_question_up_msg: `${PREFIX}get_dm_question_up_msg`,
  do_self_like_dm_item: `${PREFIX}do_self_like_dm_item`,
  get_system_dm: `${PREFIX}get_system_dm`,
}

const getSystemDm = (msg) => {
  return {
    type: DmActionType.get_system_dm,
    msg
  }
}

const initDm = (list) => {
  return {
    type: DmActionType.init_dm,
    list: list
  }
}
const insertDm = (item) => {
  return {
    type: DmActionType.insert_dm,
    item: item
  }
}
const toggleDmShow = (bool) => {
  return {
    type: DmActionType.toggle_dm,
    show: bool
  }
}
const setValue = (value) => {
  return {
    type: DmActionType.set_value,
    value:value
  }
}

const toggleDmType = (type) => {
  return {
    type: DmActionType.toggle_dm_type,
    dmType: type
  }
}

function getDmQuestionMsg({ msg, isRemoteSelf, isSelf }) {
  return {
    type: DmActionType.get_dm_question_msg,
    payload: { msg, isRemoteSelf, isSelf }
  }
}
function getDmQuestionUpMsg({ msg, isRemoteSelf, isSelf }) {
  return {
    type: DmActionType.get_dm_question_up_msg,
    payload: { msg, isRemoteSelf, isSelf }
  }
}

function getDmAnswerMsg({ msg, isRemoteSelf, isSelf }) {
  return (dispatch, getState) => {
    let _msg = singleClone(msg)
    if (_msg.type === Type.COMMON_TYPE.COMMON_DM_ANSWER) {
      _msg.data.question = getState().QuestionState.questionList.find(r => r.id === _msg.data.answer.question_id)
    }
    dispatch({
      type: DmActionType.get_dm_answer_msg,
      payload: { msg: _msg, isRemoteSelf, isSelf }
    })
  }
}

function doSelfLikeDmItem({ msgType, id }) {
  return {
    type: DmActionType.do_self_like_dm_item,
    payload: { msgType, id }
  }
}

module.exports = {
  DmActionType,
  initDm,
  insertDm,
  toggleDmShow,
  setValue,
  toggleDmType,
  getDmQuestionMsg,
  getDmAnswerMsg,
  getDmQuestionUpMsg,
  doSelfLikeDmItem,
  getSystemDm
}
